package org.mao.util;

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.metadata.Column;

import com.google.common.collect.Lists;

/**
 * 用于自动生成数据库表的实体类，映射xml，dao类，service类
 * @author admin
 *
 */
public class DBSchemaUtil extends JFrame {

	// private DBSchemaUtil util=null;
	private int width = 800;
	private int height = 630;
	private String url = null;
	private String drivername = null;
	private String username = null;
	private String password = null;
	private String dbname = null;
	private JTextField driverNameField = null;
	private JTextField urlField = null;
	private JTextField usernameField = null;
	private JTextField passwordField = null;
	private JTextField filePathField = null;
	private JTextField packageField = null;
	private JTable jtable = null;
	private DefaultTableModel tableModel = null;
	//数据库配置文件路径
	private String jdbcPropertiesFilePath = "/conf/jdbc.properties";
	//项目java源代码根路径
	private String filePath = "D:/workspace/spiderman/src/main/java/";
	//生成的类的根包名
	private String basePackage = "org.mao.forum";

	public static void main(String[] args) {
		DBSchemaUtil util = new DBSchemaUtil();
		util.setVisible(true);
		util.readJdbcProperties();
	}

	public DBSchemaUtil() {
		this.setLayout(null);
		this.setTitle("基础类生成工具");
		this.setSize(width, height);
		this.setResizable(false);
		this.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - width / 2, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - height / 2);

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		panel.setSize(width, height);
		panel.setLocation(0, 0);
		// panel.setBackground(Color.red);
		panel.setLayout(null);

		JLabel label1 = new JLabel();
		label1.setText("驱动类名");
		label1.setSize(100, 25);
		label1.setLocation(20, 20);
		panel.add(label1);

		driverNameField = new JTextField();
		driverNameField.setSize(600, 25);
		driverNameField.setLocation(100, 20);
		panel.add(driverNameField);

		JLabel label2 = new JLabel();
		label2.setText("连接字符串");
		label2.setSize(100, 25);
		label2.setLocation(20, 50);
		panel.add(label2);

		urlField = new JTextField();
		urlField.setSize(600, 25);
		urlField.setLocation(100, 50);
		panel.add(urlField);

		JLabel label3 = new JLabel();
		label3.setText("用户名");
		label3.setSize(100, 25);
		label3.setLocation(20, 80);
		panel.add(label3);

		usernameField = new JTextField();
		usernameField.setSize(600, 25);
		usernameField.setLocation(100, 80);
		panel.add(usernameField);

		JLabel label4 = new JLabel();
		label4.setText("密码");
		label4.setSize(100, 25);
		label4.setLocation(20, 110);
		panel.add(label4);

		passwordField = new JTextField();
		passwordField.setSize(600, 25);
		passwordField.setLocation(100, 110);
		panel.add(passwordField);

		JLabel label5 = new JLabel();
		label5.setText("生成文件基础路径");
		label5.setSize(100, 25);
		label5.setLocation(20, 140);
		panel.add(label5);

		filePathField = new JTextField();
		filePathField.setSize(600, 25);
		filePathField.setLocation(100, 140);
		filePathField.setText(filePath);
		panel.add(filePathField);

		JLabel label6 = new JLabel();
		label6.setText("生成文件包名");
		label6.setSize(100, 25);
		label6.setLocation(20, 170);
		panel.add(label6);

		packageField = new JTextField();
		packageField.setSize(600, 25);
		packageField.setLocation(100, 170);
		packageField.setText(basePackage);
		panel.add(packageField);

		JButton connButton = new JButton();
		connButton.setText("读取表");
		connButton.setSize(100, 30);
		connButton.setLocation(350, 205);
		connButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				// 清空表格
				tableModel.setRowCount(0);
				readTables();
			}
		});
		panel.add(connButton);

		// 初始化表头
		String[] headers = { "", "表名", "表类型", "注释名" };
		tableModel = new DefaultTableModel(null, headers) {
			// 设置第一列能修改，其余列不能修改
			public boolean isCellEditable(int row, int column) {
				if (column > 0) {
					return false;
				}
				return true;
			}

			@Override
			public Class<?> getColumnClass(int c) {
				Object value = getValueAt(0, c);
				if (value != null)
					return value.getClass();
				else
					return super.getClass();
			}
		};
		jtable = new JTable(tableModel);

		// 设置第一列不能改变宽度
		TableColumn firsetColumn = jtable.getColumnModel().getColumn(0);
		firsetColumn.setPreferredWidth(30);
		firsetColumn.setMaxWidth(30);
		firsetColumn.setMinWidth(30);
		// 表头第一列加入checkbox
		final MyCheckBoxRenderer check = new MyCheckBoxRenderer();
		firsetColumn.setHeaderRenderer(check);
		// 表头第一列checkbox添加全选事件
		jtable.getTableHeader().addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (jtable.getColumnModel().getColumnIndexAtX(e.getX()) == 0) {
					boolean b = !check.isSelected();
					check.setSelected(b);
					jtable.getTableHeader().repaint();
					for (int i = 0; i < jtable.getRowCount(); i++) {
						jtable.getModel().setValueAt(b, i, 0);// 把这一列都设成和表头一样
					}
				}
			}
		});

		// 滚动面板
		JScrollPane scrollPane = new JScrollPane(jtable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setSize(750, 300);
		scrollPane.setLocation(20, 250);
		panel.add(scrollPane);

		JButton autoButton = new JButton();
		autoButton.setText("生成");
		autoButton.setSize(100, 30);
		autoButton.setLocation(350, 560);
		autoButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				int rowcount = tableModel.getRowCount();
				for (int i = 0; i < rowcount; i++) {
					boolean checked = (boolean) tableModel.getValueAt(i, 0);
					if (checked) {
						String tableName = (String) tableModel.getValueAt(i, 1);
						System.out.println("选择了表：" + tableName);
						readColumns(tableName.toLowerCase());
					}
				}
			}
		});
		panel.add(autoButton);

		this.add(panel);
	}

	/**
	 * 读取数据库参数配置文件
	 */
	public void readJdbcProperties() {
		try {
			Properties properties = new Properties();
			InputStream is = this.getClass().getResourceAsStream(jdbcPropertiesFilePath);
			properties.load(is);
			drivername = properties.getProperty("jdbc.driverClass");
			username = properties.getProperty("jdbc.username");
			password = properties.getProperty("jdbc.password");
			url = properties.getProperty("jdbc.jdbcUrl");
			dbname = url.substring(url.lastIndexOf("/") + 1, url.indexOf("?"));
			driverNameField.setText(drivername);
			urlField.setText(url);
			usernameField.setText(username);
			passwordField.setText(password);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取数据库中的所有表和视图
	 */
	public void readTables() {
		try {
			drivername = driverNameField.getText();
			username = usernameField.getText();
			password = passwordField.getText();
			url = urlField.getText();
			dbname = url.substring(url.lastIndexOf("/") + 1, url.indexOf("?"));
			DBConnect db = new DBConnect(drivername, url, username, password);
			db.opendb();
			ResultSet rs = db.executeQuerySql("select table_name,table_type,table_comment from information_schema.tables where TABLE_SCHEMA='" + dbname + "' order by table_type,table_name");
			while (rs.next()) {
				// System.out.println(rs.getString("table_name"));
				Object[] data = new Object[4];
				data[0] = false;
				data[1] = rs.getString("table_name");
				data[2] = rs.getString("table_type");
				data[3] = rs.getString("table_comment");
				tableModel.addRow(data);
			}
			jtable.invalidate();
			rs.close();
			db.closedb();
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage(), "出错了", JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 * 读取表结构
	 * @param tableName
	 */
	public void readColumns(String tableName) {
		try {
			DBConnect db = new DBConnect(drivername, url, username, password);
			db.opendb();
			ResultSet rs = db.executeQuerySql("select * from " + tableName);
			ResultSetMetaData rsmd = rs.getMetaData();
			// int columns=rsmd.getColumnCount();
			List<Column> colList = new ArrayList<Column>();
			for (int i = 1; i <= rsmd.getColumnCount(); i++) {
				String columnName = rsmd.getColumnName(i);
				if (columnName.equalsIgnoreCase("id") || columnName.equalsIgnoreCase("createtime") || columnName.equalsIgnoreCase("createby") || columnName.equalsIgnoreCase("updatetime") || columnName.equalsIgnoreCase("updateby")) {
					continue;
				}
				Integer columnType = rsmd.getColumnType(i);
				Column col = new Column(columnName, columnType);
				colList.add(col);
			}
			rs.close();
			db.closedb();

			// 解析表名，从user_login_log变为UserLoginLog
			String[] tableNameArray = tableName.split("_");
			String fileName = "";
			if (tableNameArray.length > 1) {
				for (int i = 0; i < tableNameArray.length; i++) {
					fileName += tableNameArray[i].substring(0, 1).toUpperCase() + tableNameArray[i].substring(1, tableNameArray[i].length());
				}
			} else {
				fileName += tableNameArray[0].substring(0, 1).toUpperCase() + tableNameArray[0].substring(1, tableNameArray[0].length());
			}

			// 判断文件路径
			filePath = filePathField.getText();
			basePackage = packageField.getText();
			if (filePath.length() == 0) {
				JOptionPane.showMessageDialog(null, "请填写需要生成文件所在的路径", "出错了", JOptionPane.ERROR_MESSAGE);
			} else if (basePackage.length() == 0) {
				JOptionPane.showMessageDialog(null, "请填写需要生成文件所在的包名", "出错了", JOptionPane.ERROR_MESSAGE);
			} else {
				String basePackagePath = basePackage.replace(".", "/");
				String path = filePath + basePackagePath + "/";
				File file = new File(path);
				if (file.exists()) {
					createMapperXML(fileName, tableName, path + "sqlmap/", colList);
					createEntityJava(fileName, tableName, path + "entity/", colList);
					createDaoJava(fileName, tableName, path + "dao/", colList);
					createServiceJava(fileName, tableName, path + "service/", colList);
					createServiceImplJava(fileName, tableName, path + "service/impl/", colList);
				} else {
					int result = JOptionPane.showConfirmDialog(null, "文件夹不存在，自动创建吗?", "确认对话框", JOptionPane.YES_NO_OPTION);
					if (result == JOptionPane.YES_OPTION) {
						file.mkdirs();
						createMapperXML(fileName, tableName, path + "sqlmap/", colList);
						createEntityJava(fileName, tableName, path + "entity/", colList);
						createDaoJava(fileName, tableName, path + "dao/", colList);
						createServiceJava(fileName, tableName, path + "service/", colList);
						createServiceImplJava(fileName, tableName, path + "service/impl/", colList);
					} else if (result == JOptionPane.NO_OPTION) {

					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage(), "出错了", JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 * 生成映射文件xml
	 * @param fileName 形如UserLogin
	 * @param tableName 形如user_login
	 * @param path 形如d:\project\src\com\
	 * @param colList
	 * @throws Exception
	 */
	public void createMapperXML(String fileName, String tableName, String path, List<Column> colList) throws Exception {
		File xmlFile = new File(path);
		if (!xmlFile.exists()) {
			xmlFile.mkdirs();
		}
		String xmlfileName = fileName + "Dao.xml";
		xmlFile = new File(path + xmlfileName);
		System.out.println("开始生成：" + xmlFile.getAbsolutePath());
		FileWriter fw = new FileWriter(xmlFile);
		fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
		fw.write("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\r\n");
		fw.write("<mapper namespace=\"" + basePackage + ".dao." + fileName + "Dao\">\r\n");
		fw.write("\r\n");

		String entityName = basePackage + ".entity." + fileName;
		//生成BaseResultMap方法
		fw.write("\t<resultMap id=\"BaseResultMap\" type=\"" + entityName + "\">\r\n");
		fw.write(parseXmlResultMap(colList));
		fw.write("\t</resultMap>\r\n");
		fw.write("\r\n");
		
		//生成BaseSql方法
		fw.write("\t<sql id=\"Base_Column_List\">\r\n");
		fw.write(parseXmlBaseSql(colList));
		fw.write("\t</sql>\r\n");
		fw.write("\r\n");

		fw.write("\t<!-- 以下4个方法为basedao基础方法，必须存在，除非没有调用 -->\r\n");
		//生成list方法
		fw.write("\t<select id=\"list\" parameterType=\"boolean\" resultMap=\"BaseResultMap\">\r\n");
		fw.write("\t\tselect\r\n");
		fw.write("\t\t\t<include refid=\"Base_Column_List\"/>\r\n");
		fw.write("\t\tfrom " + tableName + "\r\n");
		fw.write("\t\t<if test=\"limit == true\">\r\n");
		fw.write("\t\t\tlimit 0, 1000\r\n");
		fw.write("\t\t</if>\r\n");
		fw.write("\t</select>\r\n");
		fw.write("\r\n");

		//生成get方法
		fw.write("\t<select id=\"get\" parameterType=\"" + entityName + "\" resultMap=\"BaseResultMap\">\r\n");
		fw.write("\t\tselect\r\n");
		fw.write("\t\t\t<include refid=\"Base_Column_List\"/>\r\n");
		fw.write("\t\tfrom " + tableName + "\r\n");
		fw.write("\t\t<where>\r\n");
		fw.write("\t\t\t<if test=\"id != null\">\r\n");
		fw.write("\t\t\t\tand id = #{id}\r\n");
		fw.write("\t\t\t</if>\r\n");
		fw.write(parseXmlGet(colList));
		fw.write("\t\t</where>\r\n");
		fw.write("\t</select>\r\n");
		fw.write("\r\n");

		//生成insert方法
		fw.write("\t<insert id=\"insert\" parameterType=\"" + entityName + "\" keyProperty=\"id\" useGeneratedKeys=\"true\">\r\n");
		fw.write("\t\tinsert into " + tableName + "\r\n");
		fw.write("\t\t<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\" >\r\n");
		fw.write(parseXmlInsert(colList));
		fw.write("\t\t</trim>\r\n");
		fw.write("\t\t<trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\" >\r\n");
		fw.write(parseXmlInsertValue(colList));
		fw.write("\t\t</trim>\r\n");
		fw.write("\t</insert>\r\n");
		fw.write("\r\n");

		//生成update方法
		fw.write("\t<update id=\"update\" parameterType=\"" + entityName + "\">\r\n");
		fw.write("\t\tupdate " + tableName + "\r\n");
		fw.write("\t\t<set>\r\n");
		fw.write(parseXmlUpdate(colList));
		fw.write("\t\t</set>\r\n");
		fw.write("\t\t<where>\r\n");
		fw.write("\t\t\t<if test=\"id != null\">\r\n");
		fw.write("\t\t\t\tand id = #{id}\r\n");
		fw.write("\t\t\t</if>\r\n");
		fw.write("\t\t</where>\r\n");
		fw.write("\t</update>\r\n");

		fw.write("\t<!-- 以上4个方法为basedao基础方法，必须存在，除非没有调用 -->\r\n");
		fw.write("\r\n");
		fw.write("</mapper>");
		fw.flush();
		fw.close();
		System.out.println("生成结束");
	}
	
	public String parseXmlResultMap(List<Column> colList) {
		String getString = "\t\t<result column=\"id\" property=\"id\" jdbcType=\"INTEGER\" />\r\n";
		for (Column col : colList) {
			String colName = col.getName();
			Integer colType = col.getType();
			getString += "\t\t<result column=\"" + colName + "\" property=\"" + colName + "\" jdbcType=\"" + convertSqlType(colType) + "\"/>\r\n";
		}
		getString += "\t\t<result column=\"createTime\" property=\"createTime\" jdbcType=\"TIMESTAMP\"/>\r\n";
		getString += "\t\t<result column=\"createBy\" property=\"createBy\" jdbcType=\"INTEGER\"/>\r\n";
		getString += "\t\t<result column=\"updateTime\" property=\"updateTime\" jdbcType=\"TIMESTAMP\"/>\r\n";
		getString += "\t\t<result column=\"updateBy\" property=\"updateBy\" jdbcType=\"INTEGER\"/>\r\n";
		return getString;
	}
	
	public String parseXmlBaseSql(List<Column> colList) {
		String getString = "id, ";
		for (Column col : colList) {
			String colName = col.getName();
			getString += colName + ", ";
		}
		getString += getString + "createTime, createBy, updateTime, updateBy";
		getString = "\t\t" + getString + "\r\n";
		return getString;
	}

	public String parseXmlGet(List<Column> colList) {
		String getString = "";
		for (Column col : colList) {
			String colName = col.getName();
			getString += "\t\t\t<if test=\"" + colName + " != null\">\r\n";
			getString += "\t\t\t\tand " + colName + " = #{" + colName + "}\r\n";
			getString += "\t\t\t</if>\r\n";
		}
		return getString;
	}

	/**
	 * fw.write("<if test=\"loginName != null\" >");
		fw.write("loginName,");
		fw.write("</if>");
	 * @param colList
	 * @return
	 */
	public String parseXmlInsert(List<Column> colList) {
		String insertString = "";
		for (Column col : colList) {
			String colName = col.getName();
			insertString += "\t\t\t<if test=\"" + colName + " != null\" >\r\n";
			insertString += "\t\t\t\t" + colName + ",\r\n";
			insertString += "\t\t\t</if>\r\n";
		}
		insertString += "\t\t\t<if test=\"createTime != null\" >\r\n";
		insertString += "\t\t\t\tcreateTime,\r\n";
		insertString += "\t\t\t</if>\r\n";
		insertString += "\t\t\t<if test=\"createBy != null\" >\r\n";
		insertString += "\t\t\t\tcreateBy,\r\n";
		insertString += "\t\t\t</if>\r\n";
		return insertString;
	}

	/**
	 * fw.write("<if test=\"loginName != null\" >");
		fw.write("#{loginName},");
		fw.write("</if>");
	 * @param colList
	 * @return
	 */
	public String parseXmlInsertValue(List<Column> colList) {
		String insertValueString = "";
		for (Column col : colList) {
			String colName = col.getName();
			insertValueString += "\t\t\t<if test=\"" + colName + " != null\" >\r\n";
			insertValueString += "\t\t\t\t#{" + colName + "},\r\n";
			insertValueString += "\t\t\t</if>\r\n";
		}
		insertValueString += "\t\t\t<if test=\"createTime != null\" >\r\n";
		insertValueString += "\t\t\t\t#{createTime},\r\n";
		insertValueString += "\t\t\t</if>\r\n";
		insertValueString += "\t\t\t<if test=\"createBy != null\" >\r\n";
		insertValueString += "\t\t\t\t#{createBy},\r\n";
		insertValueString += "\t\t\t</if>\r\n";
		return insertValueString;
	}

	/**
	 * fw.write("<if test=\"password != null\" >\r\n");
		fw.write("password = #{password},\r\n");
		fw.write("</if>\r\n");
	 * @param colList
	 * @return
	 */
	public String parseXmlUpdate(List<Column> colList) {
		String updateString = "";
		for (Column col : colList) {
			String colName = col.getName();
			updateString += "\t\t\t<if test=\"" + colName + " != null\" >\r\n";
			updateString += "\t\t\t\t" + colName + " = #{" + colName + "},\r\n";
			updateString += "\t\t\t</if>\r\n";
		}
		updateString += "\t\t\t<if test=\"updateTime != null\" >\r\n";
		updateString += "\t\t\t\tupdateTime = #{updateTime},\r\n";
		updateString += "\t\t\t</if>\r\n";
		updateString += "\t\t\t<if test=\"updateBy != null\" >\r\n";
		updateString += "\t\t\t\tupdateBy = #{updateBy},\r\n";
		updateString += "\t\t\t</if>\r\n";
		return updateString;
	}

	/**
	 * 生成实体类文件java
	 * @param fileName 形如UserLogin
	 * @param tableName 形如user_login
	 * @param path 形如d:\project\src\com\
	 * @param colList
	 * @throws Exception
	 */
	public void createEntityJava(String fileName, String tableName, String path, List<Column> colList) throws Exception {
		File javaFile = new File(path);
		if (!javaFile.exists()) {
			javaFile.mkdirs();
		}
		String javafileName = fileName + ".java";
		javaFile = new File(path + javafileName);
		System.out.println("开始生成：" + javaFile.getAbsolutePath());
		FileWriter fw = new FileWriter(javaFile);
		fw.write("package " + basePackage + ".entity;\r\n");
		fw.write("import java.util.Date;\r\n");
		fw.write("import java.math.BigDecimal;\r\n");
		fw.write("import org.mao.base.BaseEntity;\r\n");
		fw.write("public class " + fileName + " extends BaseEntity {\r\n");
		fw.write(parseJavaField(colList));
		fw.write(parseJavaGetSet(colList));
		fw.write("}\r\n");
		fw.flush();
		fw.close();
		System.out.println("生成结束");
	}

	/**
	 * private String loginName;
	 * @param colList
	 * @return
	 */
	public String parseJavaField(List<Column> colList) {
		String fieldString = "";
		for (Column col : colList) {
			String colName = col.getName();
			Integer colType = col.getType();
			fieldString += "\tprivate " + convertType(colType) + " " + colName + ";\r\n";
		}
		return fieldString;
	}

	/**
	 * fw.write("public String getLoginname() {\r\n");
		fw.write("return loginName;\r\n");
		fw.write("}\r\n");
		fw.write("public void setLoginname(String loginName) {\r\n");
		fw.write("this.loginName = loginName;\r\n");
		fw.write("}\r\n");
	 * @param colList
	 * @return
	 */
	public String parseJavaGetSet(List<Column> colList) {
		String getsetString = "";
		for (Column col : colList) {
			String colName = col.getName();
			Integer colType = col.getType();
			getsetString += "\tpublic " + convertType(colType) + " get" + colName.substring(0, 1).toUpperCase() + colName.substring(1, colName.length()) + "() {\r\n";
			getsetString += "\t\treturn " + colName + ";\r\n";
			getsetString += "\t}\r\n";
			getsetString += "\tpublic void set" + colName.substring(0, 1).toUpperCase() + colName.substring(1, colName.length()) + "(" + convertType(colType) + " " + colName + ") {\r\n";
			getsetString += "\t\tthis." + colName + " = " + colName + ";\r\n";
			getsetString += "\t}\r\n";
		}
		return getsetString;
	}

	/**
	 * 生成Dao类文件java
	 * @param fileName 形如UserLogin
	 * @param tableName 形如user_login
	 * @param path 形如d:\project\src\com\
	 * @param colList
	 * @throws Exception
	 */
	public void createDaoJava(String fileName, String tableName, String path, List<Column> colList) throws Exception {
		File javaFile = new File(path);
		if (!javaFile.exists()) {
			javaFile.mkdirs();
		}
		String javafileName = fileName + "Dao.java";
		javaFile = new File(path + javafileName);
		System.out.println("开始生成：" + javaFile.getAbsolutePath());
		FileWriter fw = new FileWriter(javaFile);
		fw.write("package " + basePackage + ".dao;\r\n");
		fw.write("import " + basePackage + ".entity." + fileName + ";\r\n");
		fw.write("import org.mao.base.BaseDao;\r\n");
		fw.write("public interface " + fileName + "Dao extends BaseDao<" + fileName + "> {\r\n");
		fw.write("}\r\n");
		fw.flush();
		fw.close();
		System.out.println("生成结束");
	}

	/**
	 * 生成Service类文件java
	 * @param fileName 形如UserLogin
	 * @param tableName 形如user_login
	 * @param path 形如d:\project\src\com\
	 * @param colList
	 * @throws Exception
	 */
	public void createServiceJava(String fileName, String tableName, String path, List<Column> colList) throws Exception {
		File javaFile = new File(path);
		if (!javaFile.exists()) {
			javaFile.mkdirs();
		}
		String javafileName = fileName + "Service.java";
		javaFile = new File(path + javafileName);
		System.out.println("开始生成：" + javaFile.getAbsolutePath());
		FileWriter fw = new FileWriter(javaFile);
		fw.write("package " + basePackage + ".service;\r\n");
		fw.write("import " + basePackage + ".entity." + fileName + ";\r\n");
		fw.write("import org.mao.base.BaseService;\r\n");
		fw.write("public interface " + fileName + "Service extends BaseService<" + fileName + "> {\r\n");
		fw.write("}\r\n");
		fw.flush();
		fw.close();
		System.out.println("生成结束");
	}

	/**
	 * 生成ServiceImpl类文件java
	 * @param fileName 形如UserLogin
	 * @param tableName 形如user_login
	 * @param path 形如d:\project\src\com\
	 * @param colList
	 * @throws Exception
	 */
	public void createServiceImplJava(String fileName, String tableName, String path, List<Column> colList) throws Exception {
		File javaFile = new File(path);
		if (!javaFile.exists()) {
			javaFile.mkdirs();
		}
		String javafileName = fileName + "ServiceImpl.java";
		javaFile = new File(path + javafileName);
		System.out.println("开始生成：" + javaFile.getAbsolutePath());
		FileWriter fw = new FileWriter(javaFile);
		fw.write("package " + basePackage + ".service.impl;\r\n");
		fw.write("import " + basePackage + ".dao." + fileName + "Dao;\r\n");
		fw.write("import " + basePackage + ".entity." + fileName + ";\r\n");
		fw.write("import " + basePackage + ".service." + fileName + "Service;\r\n");
		fw.write("import org.mao.base.BaseServiceImpl;\r\n");
		fw.write("import org.springframework.beans.factory.annotation.Autowired;\r\n");
		fw.write("import org.springframework.stereotype.Service;\r\n");
		fw.write("import org.springframework.transaction.annotation.Transactional;\r\n");
		fw.write("@Service\r\n");
		fw.write("@Transactional(rollbackFor = Exception.class)\r\n");
		fw.write("public class " + fileName + "ServiceImpl extends BaseServiceImpl<" + fileName + "> implements " + fileName + "Service {\r\n");
		fw.write("\t@Autowired\r\n");
		fw.write("\tprivate " + fileName + "Dao " + fileName.substring(0, 1).toLowerCase() + fileName.substring(1, fileName.length()) + "Dao;\r\n");
		fw.write("}\r\n");
		fw.flush();
		fw.close();
		System.out.println("生成结束");
	}

	public String convertType(Integer sqlType) {
		switch (sqlType) {
		case java.sql.Types.VARCHAR:
		case java.sql.Types.CHAR:
			return "String";
		case java.sql.Types.BLOB:
		case java.sql.Types.BINARY:
			return "byte[]";
		case java.sql.Types.BIT:
		case java.sql.Types.TINYINT:
		case java.sql.Types.SMALLINT:
		case java.sql.Types.INTEGER:
			return "Integer";
		case java.sql.Types.DECIMAL:
		case java.sql.Types.DOUBLE:
		case java.sql.Types.FLOAT:
			return "BigDecimal";
		case java.sql.Types.DATE:
		case java.sql.Types.TIMESTAMP:
			return "Date";
		}
		return null;
	}

	public String convertSqlType(Integer sqlType) {
		switch (sqlType) {
		case java.sql.Types.VARCHAR:
		case java.sql.Types.CHAR:
			return "VARCHAR";
		case java.sql.Types.BIT:
		case java.sql.Types.TINYINT:
		case java.sql.Types.SMALLINT:
		case java.sql.Types.INTEGER:
			return "INTEGER";
		case java.sql.Types.DECIMAL:
		case java.sql.Types.DOUBLE:
		case java.sql.Types.FLOAT:
			return "DECIMAL";
		case java.sql.Types.DATE:
		case java.sql.Types.TIMESTAMP:
			return "TIMESTAMP";
		}
		return null;
	}

	class MyCheckBoxRenderer extends JCheckBox implements TableCellRenderer {

		public MyCheckBoxRenderer() {
			this.setBorderPainted(true);
			this.setHorizontalAlignment(this.CENTER);
		}

		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
			// TODO Auto-generated method stub
			return this;
		}
	}

}
